﻿using System;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ProjectMadaktariMVC.Models;
using System.Collections.Generic;

namespace ProjectMadaktariMVC.Helpers
{
    public class ConfigurationHelper
    {

        public static string HospitalDropDown(MadaktariDataContext db, string name, int? HospitalId)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);

            foreach (Hospital h in ConfigurationDAO.GetHospitals(db))
            {
                sb.Append("<option value=\"").Append(h.Id).Append("\" ");
                if (h.Id == HospitalId)
                    sb.Append("selected=\"true\" ");
                sb.Append(">").Append(h.Name).Append("</option>\n");
            }

            sb.Append("</select>\n");
            return sb.ToString();
        }

        public static string HospitalDropDown(MadaktariDataContext db, string name)
        {
            return HospitalDropDown(db, name, -1);
        }

        public static string referralLocationDropdown(MadaktariDataContext db, string name, int? referralLocationId)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);

            foreach (ReferralLocation rf in ConfigurationDAO.GetreferralLocations(db))
            {
                sb.Append("<option value=\"").Append(rf.Id).Append("\" ");
                if (rf.Id == referralLocationId)
                    sb.Append("selected=\"true\" ");
                sb.Append(">").Append(rf.Name).Append("</option>\n");
            }

            sb.Append("</select>\n");


            return sb.ToString();
        }

        public static string referralLocationDropdown(MadaktariDataContext db, string name)
        {
            return referralLocationDropdown(db, name, -1);
        }

        public static string DiagnosisList(MadaktariDataContext db, string name, int EpisodeId)
        {
            PatientDAO patientDAO = new PatientDAO();
            StringBuilder sb = new StringBuilder();

            var diags = ConfigurationDAO.GetDiagnoses(db);
            var groups = diags.Select(x => x.Category).Distinct();

            foreach (var group in groups)
            {
                var gd = diags.Where(x => x.Category == group).OrderBy(x => x.Name);

                StringBuilder sbRight = new StringBuilder();
                StringBuilder sbLeft = new StringBuilder();
                int i = 1;

                foreach (Diagnosi d in gd)
                {
                    StringBuilder tempSb;

                    if (i % 2 == 0)
                    {
                        tempSb = sbRight;
                    }
                    else
                    {
                        tempSb = sbLeft;
                    }
                    tempSb.AppendFormat("<input type=\"checkbox\" id=\"diag{1}\" name=\"{0}\" value=\"{1}\"", name, d.Id);
                    if (patientDAO.EpisodeHasDiagnosis(EpisodeId, d.Id))
                    {
                        tempSb.Append("checked=\"true\" ");
                    }
                    tempSb.Append("/>");
                    tempSb.AppendFormat("<label for=\"diag{0}\">{1}</label>", d.Id, d.Name);
                    tempSb.Append("<br />");
                    i++;
                }

                if (!string.IsNullOrEmpty(group))
                {
                    sb.AppendFormat("<div class=\"blockHeader\">{0}</div>", group);
                }
                sb.Append("<div class=\"block1\">").Append(sbLeft.ToString()).Append("</div><div class=\"block2\">").Append(sbRight.ToString()).Append("</div>");
            }

            return sb.ToString();
        }

        public static string DiagnosisList(MadaktariDataContext db, string name)
        {
            return DiagnosisList(db, name, -1);
        }

        public static string ConcomitantDiagnosisDropDown(MadaktariDataContext db, string name, int? concomitantDiagnosisId)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);

            sb.Append("<option></option>\n");

            foreach (ConcomitantDiagnosi cd in ConfigurationDAO.GetConcomitantDiagnoses(db))
            {
                sb.Append("<option value=\"").Append(cd.Id).Append("\" ");
                if (cd.Id == concomitantDiagnosisId)
                {
                    sb.Append("selected=\"true\" ");
                }
                sb.Append(">").Append(cd.Name).Append("</option>\n");
            }

            sb.Append("</select>\n");

            return sb.ToString();
        }

        //Todo: use data driven values
        public static string PostOpStatusDropDown(MadaktariDataContext db, string name, string value)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);
            string[] postOpStatuses = { "Doing Fine", "Stable", "Unstable", "Critical" };
            sb.Append("<option value=\"\"></option>\n");
            foreach (string s in postOpStatuses)
            {
                sb.Append("<option value=\"").Append(s).Append("\" ");
                if (s.Equals(value))
                {
                    sb.Append("selected=\"true\" ");
                }
                sb.Append(" >").Append(s).Append("</option>\n");
            }

            sb.Append("</select>\n");

            return sb.ToString();
        }

        public static string ComplicationsRadioButtons(string name, bool? value)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<input type=\"radio\" value=\"true\" name=\"").Append(name).Append("\" ");
            if (value == true)
            {
                sb.Append("checked=\"true\" ");
            }
            sb.Append("/> Yes <input type=\"radio\" value=\"false\" name=\"").Append(name).Append("\" ");
            if (value == false)
            {
                sb.Append("checked=\"true\" ");
            }

            sb.Append("/> No");

            return sb.ToString();
        }

        public static string FollowUpStatusDropdown(MadaktariDataContext db, string name, string value)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);
            string[] statuses = { "Functional", "Slightly Disabled", "Disabled" };
            sb.Append("<option value=\"\"></option>");
            foreach (string s in statuses)
            {
                sb.Append("<option value=\"").Append(s).Append("\" ");
                if (s.Equals(value))
                {
                    sb.Append("selected=\"true\" ");
                }
                sb.Append(">").Append(s).Append("</option>\n");
            }

            sb.Append("</select>");
            return sb.ToString();
        }

        public static string AnesthesiaRadioButtons(MadaktariDataContext db, string name, string value)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);
            string[] statuses = { "Yes", "No", "Maybe" };
            sb.Append("<option value=\"\"></option>");
            foreach (string s in statuses)
            {
                sb.Append("<option value=\"").Append(s).Append("\" ");
                if (s.Equals(value))
                {
                    sb.Append("selected=\"true\" ");
                }
                sb.Append(">").Append(s).Append("</option>\n");
            }

            sb.Append("</select>\n");
            return sb.ToString();
        }

        public static string SurgicalComplicationsRadioButtons(MadaktariDataContext db, string name, bool? value)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("<input type=\"radio\" value=\"true\" name=\"").Append(name).Append("\" ");
            if (value == true)
            {
                sb.Append("checked=\"true\" ");
            }
            sb.Append("/> Yes <input type=\"radio\" value=\"false\" name=\"").Append(name).Append("\" ");
            if (value == false)
            {
                sb.Append("checked=\"true\" ");
            }

            sb.Append("/> No");

            return sb.ToString();
        }

        public static string ProcedureDropDown(MadaktariDataContext db, string name, int? procedureTypeId)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);

            foreach (ProcedureType pt in ConfigurationDAO.GetProcedureTypes(db))
            {
                sb.Append("<option value=\"").Append(pt.Id).Append("\" ");
                if (pt.Id == procedureTypeId)
                    sb.Append("selected=\"true\" ");
                sb.Append(">").Append(pt.Name).Append("</option>\n");
            }

            sb.Append("</select>\n");
            return sb.ToString();
        }

        public static string ProcedureDropDown(MadaktariDataContext db, string name)
        {
            return ProcedureDropDown(db, name, -1);
        }

        public static string SurgeonDropDown(MadaktariDataContext db, string name, int? surgeonId)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);

            foreach (Surgeon s in ConfigurationDAO.GetSurgeons(db))
            {
                sb.Append("<option value=\"").Append(s.Id).Append("\" ");
                if (s.Id == surgeonId)
                    sb.Append("selected=\"true\" ");
                sb.Append(">").Append(s.FirstName).Append(" ").Append(s.LastName).Append("</option>\n");
            }

            sb.Append("</select>\n");
            return sb.ToString();
        }

        public static string SurgeonDropDown(MadaktariDataContext db, string name)
        {
            return SurgeonDropDown(db, name, -1);
        }

        public static string TribeDropDown(MadaktariDataContext db, string name, string tribeName)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<select id=\"{0}\" name=\"{0}\">\n", name);

            sb.Append("<option></option>\n");

            foreach (Tribe cd in ConfigurationDAO.GetTribes(db))
            {
                sb.Append("<option value=\"").Append(cd.Id).Append("\" ");
                if (cd.Name == tribeName)
                {
                    sb.Append("selected=\"true\" ");
                }
                sb.Append(">").Append(cd.Name).Append("</option>\n");
            }

            sb.Append("</select>\n");

            return sb.ToString();
        }
    }
}
